package com.bjpowernode.crm.workbench.controller;

import com.bjpowernode.crm.commons.contans.Contans;
import com.bjpowernode.crm.commons.domain.ReturnObject;
import com.bjpowernode.crm.commons.utils.DateUtils;
import com.bjpowernode.crm.commons.utils.UUIDUtils;
import com.bjpowernode.crm.settings.domain.User;
import com.bjpowernode.crm.settings.service.UserService;
import com.bjpowernode.crm.workbench.domain.Activity;
import com.bjpowernode.crm.workbench.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@RequestMapping("/workbench/activity/")
public class ActivityController {

    @Autowired
    private UserService userService;

    @Autowired
    private ActivityService activityService;

    /**
     * 展示用户信息
     * @param request
     * @return
     */
    @RequestMapping("index.do")
    public String index(HttpServletRequest request){
        //调用service层的查询方法,查询所有用户
        List<User> userList = userService.queryAllUsers();
        //保存数据到request中
        request.setAttribute("userList",userList);
        //请求转发到市场活动页面
        return "workbench/activity/index";
    }

    @RequestMapping("saveCreateActivity.do")
    public @ResponseBody Object saveCreateActivity(Activity activity, HttpSession session){
        //封装传入的参数(随机生成一个)
        activity.setId(UUIDUtils.getUUID());
        //传入系统当前时间
        activity.setCreateTime(DateUtils.formateDateTime(new Date()));
        //传入当前登录的用户
        User userName = (User)session.getAttribute(Contans.SESSION_USER);
        activity.setCreateBy(userName.getId());

        //封装返回信息
        ReturnObject returnObject = new ReturnObject();
        try {
            //调用service层方法,保存数据
            int ret = activityService.SaveCreateActivity(activity);
            if(ret > 0){
                returnObject.setCode(Contans.RETURN_OBJECT_CODE_SUCCESS);
                returnObject.setMessage("添加成功");
                return returnObject;
            }else{
                returnObject.setCode(Contans.RETURN_OBJECT_CODE_FALSE);
                returnObject.setMessage("保存失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            returnObject.setCode(Contans.RETURN_OBJECT_CODE_FALSE);
            returnObject.setMessage("保存失败");
        }

        return returnObject;
    }


    /**
     * 分页、查询
     * @return
     */
    @RequestMapping("queryActivityByConditionForPAge.do")
    @ResponseBody
    public Object queryActivityByConditionForPAge(String name,String owner,String startDate,String endDate
    ,int pageNo,int pageSize){
        //封装参数
        Map<String, Object> map = new HashMap<>();
        map.put("name",name);
        map.put("owner",owner);
        map.put("startDate",startDate);
        map.put("endDate",endDate);
        map.put("beginNo",(pageNo-1)*pageSize);
        map.put("pageSize",pageSize);

        //调用service层方法去查数据
        List<Activity> activityList = activityService.queryActivityByConditionForPage(map);
        //查询页面数量
        int totalRows = activityService.queryCountOfActivityByCondition(map);

        //生成响应信息
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("activityList",activityList);
        resultMap.put("totalRows",totalRows);
        return resultMap;
    }

    /**
     * 批量删除数据
     * @param id
     * @return
     */
    @RequestMapping("deleteActivityIds.do")
    @ResponseBody
    public Object deleteActivityIds(String[] id){
        //创建一个返回对象
        ReturnObject returnObject = new ReturnObject();
        try {
                //调用service层方法去执行函数
                int res = activityService.deleteActivityByIds(id);
                if (res > 0) {
                    returnObject.setCode(Contans.RETURN_OBJECT_CODE_SUCCESS);
                    returnObject.setMessage("删除成功！");
                    return returnObject;
                } else {
                    returnObject.setCode(Contans.RETURN_OBJECT_CODE_FALSE);
                    returnObject.setMessage("删除失败！");
                    return returnObject;
                }
        }catch (Exception e){
            e.printStackTrace();
            returnObject.setCode(Contans.RETURN_OBJECT_CODE_FALSE);
            returnObject.setMessage("删除失败！");
            return returnObject;
        }

    }

    /**
     * 根据id查询市场活动，然后在前端显示出来
     * @param id
     * @return
     */
    @RequestMapping("queryActivityById.do")
    @ResponseBody
    public Object queryActivityById(String id){
        //先查询有没有该市场活动，
        Activity activity = activityService.selectActivityById(id);
        //获得activity对象并且封装好
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("id",activity.getId());
        resultMap.put("owner",activity.getOwner());
        resultMap.put("name",activity.getName());
        resultMap.put("startDate",activity.getStartDate());
        resultMap.put("endDate",activity.getEndDate());
        resultMap.put("cost",activity.getCost());
        resultMap.put("description",activity.getDescription());
        return resultMap;
    }


    @RequestMapping("saveEditActivity")
    @ResponseBody
    public Object saveEditActivity(Activity activity,HttpSession session){
        //封装修改时间
        activity.setEditTime(DateUtils.formateDateTime(new Date()));
        User user =(User) session.getAttribute(Contans.SESSION_USER);
        //用id因为姓名不唯一
        activity.setEditBy(user.getId());
        //调用service层
        int i = activityService.saveEditActivity(activity);
        //创建返回信息类
        ReturnObject returnObject = new ReturnObject();

        try{
            if(i > 0){
                returnObject.setCode(Contans.RETURN_OBJECT_CODE_SUCCESS);
                returnObject.setMessage("更新成功！");
                return returnObject;
            }else {
                returnObject.setCode(Contans.RETURN_OBJECT_CODE_FALSE);
                returnObject.setMessage("更新失败！");
                return returnObject;
            }
        }catch (Exception e){
            e.printStackTrace();
            returnObject.setCode(Contans.RETURN_OBJECT_CODE_FALSE);
            returnObject.setMessage("更新失败！");
            return returnObject;
        }

    }











}
